package vn.com.demand.lux.preferences;

import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import vn.com.demand.lux.Activator;

import com.swtdesigner.FieldLayoutPreferencePage;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class XSLMappingFilesPreference extends FieldLayoutPreferencePage
		implements IWorkbenchPreferencePage {

	private FileFieldEditor xslWarrantyAccrualMappingLocation;
	private FileFieldEditor xslWarrantyReservalMappingLocation;
	private FileFieldEditor xslAccountReceivableMappingLocation;
	private FileFieldEditor xslInterestForwardMappingLocation;

	/**
	 * Create the preference page
	 */
	public XSLMappingFilesPreference() {
		super();
		setTitle("XSL Mapping Files");
		setDescription("Enter the location of files");
		setImageDescriptor(ResourceManager.getPluginImageDescriptor(Activator.getDefault(), "icons/actions/tab_new_raised.png"));
	}

	/**
	 * Create contents of the preference page
	 * @param parent
	 */
	@Override
	public Control createPageContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
		fillLayout.spacing = 5;
		container.setLayout(fillLayout);

		final Group accountReceivableGroup = new Group(container, SWT.NONE);
		accountReceivableGroup.setForeground(SWTResourceManager.getColor(0, 0, 255));
		accountReceivableGroup.setText("AccountReceivable Mapping");
		accountReceivableGroup.setLayout(new FormLayout());

		final Composite accountReceivableMapping = new Composite(accountReceivableGroup, SWT.NONE);
		final FormData fd_accountReceivableMapping = new FormData();
		fd_accountReceivableMapping.bottom = new FormAttachment(100, -27);
		fd_accountReceivableMapping.right = new FormAttachment(100, -5);
		fd_accountReceivableMapping.top = new FormAttachment(0, 15);
		fd_accountReceivableMapping.left = new FormAttachment(0, 5);
		accountReceivableMapping.setLayoutData(fd_accountReceivableMapping);

		xslAccountReceivableMappingLocation = new FileFieldEditor(PreferenceConstants.XSL_ACCOUNTRECEIVABLEMAPPING, "Location:", accountReceivableMapping);
		xslAccountReceivableMappingLocation.setFileExtensions(new String[] {"*.xsl"});
		xslAccountReceivableMappingLocation.setPreferenceStore(getPreferenceStore());
		xslAccountReceivableMappingLocation.load();
		xslAccountReceivableMappingLocation.setValidateStrategy(FileFieldEditor.VALIDATE_ON_FOCUS_LOST);

		final Group interestForwardGroup = new Group(container, SWT.NONE);
		interestForwardGroup.setText("Interest Forward Mapping");
		interestForwardGroup.setLayout(new FormLayout());

		final Composite interestForwardMapping = new Composite(interestForwardGroup, SWT.NONE);
		final FormData fd_interestForwardMapping = new FormData();
		fd_interestForwardMapping.bottom = new FormAttachment(0, 40);
		fd_interestForwardMapping.right = new FormAttachment(100, -5);
		fd_interestForwardMapping.top = new FormAttachment(0, 15);
		fd_interestForwardMapping.left = new FormAttachment(0, 5);
		interestForwardMapping.setLayoutData(fd_interestForwardMapping);

		xslInterestForwardMappingLocation = new FileFieldEditor(PreferenceConstants.XSL_INTERESTFORWARD, "Location:", interestForwardMapping);
		xslInterestForwardMappingLocation.setFileExtensions(new String[] {"*.xsl"});
		xslInterestForwardMappingLocation.setPreferenceStore(getPreferenceStore());
		xslInterestForwardMappingLocation.load();
		xslInterestForwardMappingLocation.setValidateStrategy(FileFieldEditor.VALIDATE_ON_FOCUS_LOST);

		final Group warrantyReservelMappingGroup = new Group(container, SWT.NONE);
		warrantyReservelMappingGroup.setLayout(new FormLayout());
		warrantyReservelMappingGroup.setText("Warranty Accrual Mapping");

		final Composite warrantyAccrualMapping = new Composite(warrantyReservelMappingGroup, SWT.NONE);
		final FormData fd_warrantyAccrualMapping = new FormData();
		fd_warrantyAccrualMapping.right = new FormAttachment(100, -5);
		fd_warrantyAccrualMapping.bottom = new FormAttachment(0, 40);
		fd_warrantyAccrualMapping.top = new FormAttachment(0, 15);
		fd_warrantyAccrualMapping.left = new FormAttachment(0, 5);
		warrantyAccrualMapping.setLayoutData(fd_warrantyAccrualMapping);

		xslWarrantyAccrualMappingLocation = new FileFieldEditor(PreferenceConstants.XSL_WARRANTYACCRUAL, "Location", warrantyAccrualMapping);
		xslWarrantyAccrualMappingLocation.setFileExtensions(new String[] {"*.xsl"});
		xslWarrantyAccrualMappingLocation.setPreferenceStore(getPreferenceStore());
		xslWarrantyAccrualMappingLocation.load();

		final Group warrantyReservalGroup = new Group(container, SWT.NONE);
		warrantyReservalGroup.setLayout(new FormLayout());
		warrantyReservalGroup.setText("Warranty Reserval Mapping");

		final Composite warrantyReservalMapping = new Composite(warrantyReservalGroup, SWT.NONE);
		final FormData fd_warrantyReservalMapping = new FormData();
		fd_warrantyReservalMapping.right = new FormAttachment(100, -2);
		fd_warrantyReservalMapping.bottom = new FormAttachment(0, 40);
		fd_warrantyReservalMapping.top = new FormAttachment(0, 15);
		fd_warrantyReservalMapping.left = new FormAttachment(0, 5);
		warrantyReservalMapping.setLayoutData(fd_warrantyReservalMapping);

		xslWarrantyReservalMappingLocation = new FileFieldEditor(PreferenceConstants.XSL_WARRANTYRESERVAL, "Location", warrantyReservalMapping);
		xslWarrantyReservalMappingLocation.setFileExtensions(new String[] {"*.xsl"});
		xslWarrantyReservalMappingLocation.setPreferenceStore(getPreferenceStore());
		xslWarrantyReservalMappingLocation.load();

		return container;
	}
	
	
	
	@Override
	protected void checkState() {
		super.checkState();
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		super.propertyChange(event);
	}

	/**
	 * Initialize the preference page
	 */
	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}
	
	@Override
	public boolean performOk() {
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XSL_ACCOUNTRECEIVABLEMAPPING, xslAccountReceivableMappingLocation.getStringValue());
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XSL_INTERESTFORWARD, xslInterestForwardMappingLocation.getStringValue());
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XSL_WARRANTYACCRUAL, xslWarrantyAccrualMappingLocation.getStringValue());
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XSL_WARRANTYRESERVAL, xslWarrantyReservalMappingLocation.getStringValue());
		this.performApply();
		return super.performOk();
	}

	@Override
	protected void performApply() {
		super.performApply();
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XSL_ACCOUNTRECEIVABLEMAPPING, xslAccountReceivableMappingLocation.getStringValue());
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XSL_INTERESTFORWARD, xslInterestForwardMappingLocation.getStringValue());
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XSL_WARRANTYACCRUAL, xslWarrantyAccrualMappingLocation.getStringValue());
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.XSL_WARRANTYRESERVAL, xslWarrantyReservalMappingLocation.getStringValue());
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
		xslAccountReceivableMappingLocation.loadDefault();
		xslInterestForwardMappingLocation.loadDefault();
		xslWarrantyAccrualMappingLocation.loadDefault();
		xslWarrantyReservalMappingLocation.loadDefault();
	}
	
	
	
}
